import React from 'react';
import { observer } from 'mobx-react';
import { WeaTools,WeaErrorPage,WeaSearchGroup,WeaFormItem,WeaSelect,WeaLocaleProvider } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import CustomConditionItem from'../../public/wf-custom-query/index.js';
import isEmpty from 'lodash/isEmpty';
import * as _ from 'lodash';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class Condition extends React.Component{

    constructor(){
        super();
        this.fixedFormParams = {};
        this.customFormParams = {};
    }

    render(){
        let group = [];
        const { queryStore } = this.props;
        const { fixedCondition,customid,customOption,customCondition,fixedForm,customForm,belongPathTree,wfBrowserLimit,viewcondition } = queryStore;
        /**常用查询条件 */
        const fixedFormParamsTemp = fixedForm.getFormParams();
        for(let param in fixedFormParamsTemp){
            this.fixedFormParams[param] = fixedFormParamsTemp[param];
        }
        fixedForm && fixedForm.isFormInit && fixedCondition.map((v,index) =>{
            let items = [];
            v.items.map(item => {
                if(item.domkey[0] === "typeid" && !belongPathTree)     //去除类型条件
                    return true;
                if(item.domkey[0] === "wfstatu" && viewcondition == 1)     //去除处理状态条件
                    return true;
                if(item.domkey[0] === "workflowid"){    //限制路径可选范围
                    if(belongPathTree){
                        item.browserConditionParam.dataParams = {};
                        item.browserConditionParam.completeParams = {};
                    }else{
                        item.browserConditionParam.dataParams = wfBrowserLimit;
                        item.browserConditionParam.completeParams = wfBrowserLimit;
                    }
                }
                items.push({
                    com:(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@edru1y@${item.domkey[0]}`}
                            label={`${item.label}`}
                            labelCol={{span: `${item.labelcol}`}}
                            wrapperCol={{span: `${item.fieldcol}`}}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@6be9ub@${item.domkey[0]}`} fieldConfig={item} form={fixedForm} formParams={this.fixedFormParams} onChange={this.changeEvent} onEnterSearch={this.onEnterSearch} />
                        </WeaFormItem>),
                    colSpan:1
                });
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@z9bbmv@${index}`} needTigger={true} title={v.title} showGroup={v.defaultshow} items={items}/>);
        });
        /**自定义查询条件 */
        if(customOption && customOption.length > 0){  //自定义查询条件
            const customFormParamsTemp = customForm && customForm.getFormParams();
            for(let param in customFormParamsTemp){
                this.customFormParams[param] = customFormParamsTemp[param];
            }
            let customItems = [];
            customForm && customForm.isFormInit && customCondition.length > 0 && customCondition.map(item =>{
                customItems.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@5qye8p@${item.domkey[0]}`}
                            label={`${item.label}`}
                            labelCol={{span: `${item.labelcol}`}}
                            wrapperCol={{span: `${item.fieldcol}`}}>
                            {
                                item.conditionType === "CUSTOM" ?
                                    <CustomConditionItem ecId={`${this && this.props && this.props.ecId || ''}_CustomConditionItem@styo8y@${item.domkey[0]}`} config={item} form={customForm} /> :
                                    <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@7jgpo9@${item.domkey[0]}`} fieldConfig={item} form={customForm} formParams={this.customFormParams} onEnterSearch={this.onEnterSearch} />
                            }  
                        </WeaFormItem>)
                });
            });
            let customComponent = <div style={{width:"220px",float:"right"}}>
                <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@bct4i3`} options={mobx.toJS(customOption)} value={customid} onChange={this.onChange} />
            </div>;
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ke0dfl@customCondition`} needTigger={true} title={getLabel(84557,"自定义查询条件")} showGroup={true} items={customItems} customComponent={customComponent}/>);
        }
        return <div>{group}</div>;
    }
    onChange = (v)=>{this.props.queryStore.changeCustomid(v)};
    changeEvent = (obj) => {
        if(!obj || isEmpty(obj))
            return;
        const { queryStore } = this.props;
        const fixedFormParams = queryStore.fixedForm.getFormParams();
        if("workflowid" in obj){      //改变了工作流条件
            const oldWfid = fixedFormParams && fixedFormParams["workflowid"] || "";
            const newWfid = obj["workflowid"].value;
            //if(oldWfid != newWfid)
                queryStore.changeWorkflowid(newWfid);
        }
    }

    onEnterSearch = () =>{
        const { onEnterSearch } = this.props;
        onEnterSearch();
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@zjai86`} msg={ props.error ? props.error : getLabel(383324,' 对不起，该页面异常，请联系管理员 ')+'！'} />,
    {error: ""}
)(Condition);